<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Model;
use Api\Logic\UserLogic;

/**
 * 小哥控制器
 * @author derek <693472824@qq.com>
 */
class WorkerController extends BaseController {
    
    /**
     * 列表
     * @author derek <693472824@qq.com>
     */
    public function index(){
        
        $this->display();
    }
    
    /**
     * ajax 小哥列表
     * @author derek <693472824@qq.com>
     */
    public function ajax_workerList(){
    
        $start = I('lastIndex');
        $start = empty($start) || !is_numeric($start) ? '0' : $start;
        
        $view = D('WorkerView');
        
        $total = $view->count();
        
        if($start <= $total){
        
            $list = $view->limit($start, $this->limit)->order('score DESC,orders_sum DESC')
                        ->getField('worker_id,account,name,eid,profession,score,orders_sum,fullpath');
            
            if(!empty($list)){
                
                foreach ($list as $key=>&$val){
                    
                    $val['profession_str'] = get_dictionary_enum_name('profession_options', strrchr($val['profession'], ','));
                }
                
                $this->result['status'] = '1';
                $list = array_values($list);
                $this->result['info'] = $list;
            }
        }
        
        
        if($start + $this->limit >= $total){
            $this->result['is_last'] = '1';
        }
        
        $this->ajaxReturn($this->result);
    }
    
    /**
     * 查看小哥详情
     */
    public function viewAction(){
        
        $workerID = I('worker_id');
        
        if(empty($workerID)){
            $this->error('参数错误', U('Worker/index'));
        }
        
        $map['worker_id'] = $workerID;
        
        $worker = D('WorkerView')->where($map)
                    ->field('worker_id,account,name,eid,profession,work_year,score,orders_sum,avg_hour,description,fullpath')
                    ->find();
        
        $total = M('WorkerEvaluation')->where($map)->count("ev_id");
        
        $this->assign('total', $total);
        $this->assign('workerModel', $worker);
        $this->display();
    }
    
    /**
     * 查看评价列表
     */
    public function ajax_evaluationList(){
        
        $workerID = I('worker_id');
        
        if(empty($workerID)){
            $this->result['status'] = 0;
            $this->result['msg'] = '参数错误';
        }else{
            
            $start = I('lastIndex');
            $start = empty($start) || !is_numeric($start) ? '0' : $start;
            
            $view = D('EvaluationView');
            
            $map['worker_id'] = $workerID;
            $total = $view->where($map)->count();
            
            if($start <= $total){
            
                $list = $view->where($map)->limit($start, $this->limit)->order('create_time DESC')
                            ->getField('ev_id,score,content,create_time,username,fullpath');
                
                if(!empty($list)){
                    $this->result['status'] = '1';
                    $list = array_values($list);
                    $this->result['info'] = $list;
                }
            }
            
            if($start + $this->limit >= $total){
                $this->result['is_last'] = '1';
            }
        }
        
        $this->ajaxReturn($this->result);
    }
    
    /**
     * 下订单时指定小哥
     */
    public function orderWorker(){
        
        $data = I('data');
        
        if(!empty($data)){
            $this->assign('data', $data);
        }else{
            $data = I();
            $this->assign('data', serialize($data));
        }
        
        $param = I('param');
        
        if(!empty($param)){
            
            $paramMap['eid'] = array('like', '%'.(string)$param.'%');
            $paramMap['status'] = '1';
            $worker = M('Worker')->where($paramMap)->find();
            
            if(!empty($worker)){
                
                $map['worker_id'] = $worker['worker_id'];

                $dateList = D('Worker', 'Logic')->schedule($worker['worker_id']);
                $worker = D('WorkerView')->where($map)
                        ->field('worker_id,account,name,eid,job,profession,sex,score,orders_sum,member_type,status,fullpath')->find();
                $total = M('WorkerEvaluation')->where($map)->count("ev_id");
                
                $this->assign('dateList', $dateList);
                $this->assign('workerModel', $worker);
                $this->assign('total', $total);
            }
        }
        
        $this->assign('param', $param);
        $this->display('orderWorker');
    }
    
    /**
     * 指定小哥后下单
     */
    public function order(){
        
        $data = I();
        
        if(empty($data['worker_id']) || empty($data['appointment'])){
            $this->error('参数错误', U('Index/index'));
        }
        
        $param = $data['data'];
        $param = str_replace('&quot;', '"', $param);
        $param = unserialize($param);
        $param['worker_id'] = $data['worker_id'];
        $param['appointment'] = $data['appointment'];
        
        $this->redirect('Orders/appointment', $param);
    }
    
    /**
     * 评价小哥
     */
    public function comment(){
        
        if(IS_AJAX){
            
            $orderID = I('order_id');
            $data['score'] = I('score');
            $data['content'] = I('content');
            
            $userID = UserLogic::is_login();
            $order = M('Orders')->where(array('order_id'=>$orderID, 'user_id'=>$userID))->find();
            
            if(empty($userID)){
            
                $this->result['status'] = '-201';
            }elseif(empty($order) || empty($data['score'])){
            
                $this->result['status'] = '0';
            }elseif($order['status'] == '9'){
            
                $this->result['status'] = '-1';
            }else{
                $data['user_id'] = $userID;
                $data['worker_id'] = $order['worker_id'];
                $data['order_id'] = $orderID;
                $data['create_time'] = NOW_TIME;
                
                $model = M('WorkerEvaluation');
                $model->add($data);
                
                //更新小哥评分
                $score = $model->where(array('worker_id'=>$order['worker_id']))->avg('score');
                $score = $score ? round($score, 0): '9';
                M('Worker')->where(array('worker_id'=>$order['worker_id']))->setField('score', $score);
                
                M('Orders')->where(array('order_id'=>$orderID, 'user_id'=>$userID))->setField('status', '9');
                
                $this->result['status'] = '1';
            }
            
            $this->ajaxReturn($this->result);
        }else{
            
            $orderID = I('order_id');
            $userID = $this->checkLogin('Worker/comment', array('order_id'=>$orderID));
            
            $map['order_id'] = $orderID;
            $map['user_id'] = $userID;
            $order = M('Orders')->where($map)->find();
            
            if(!empty($order)){
                
                $worker = D('WorkerView')->where(array('worker_id'=>$order['worker_id']))->find();
                
                $this->assign('order_id', $orderID);
                $this->assign('workerModel', $worker);
                $this->display();
            }else{
                $this->error('参数错误');
            }
        }
        
    }
}
